package com.genius.mq;

import cn.hutool.json.JSONUtil;
import com.genius.auth.qo.EmailQO;
import com.genius.utils.EmailUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 短信监听消费者
 */
@Slf4j
@Component
public class EmailMQConsumer {

    @Autowired
    private EmailUtils emailUtils;

    /**
     * 监听队列，并且处理消息
     * @param payload 载体
     * @param message 信息
     */
    @RabbitListener(queues = {EmailMQConfig.EMAIL_QUEUE})
    public void watchQueue(String payload, Message message) {

        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        // 处理用户注册验证消息
        if (routingKey.equalsIgnoreCase(EmailMQConfig.ROUTING_KEY_EMAIL_SEND_REGISTER)) {
            // 此处为短信发送的消息消费处理
            EmailQO emailQO = JSONUtil.toBean(payload, EmailQO.class);
            emailUtils.htmlEmail(emailQO);
        }
    }

}
